<?php namespace Phpcmf\Controllers\Admin;

class Home extends \Phpcmf\App
{

    public function __construct(...$params)
    {
        parent::__construct(...$params);
        \Phpcmf\Service::V()->assign([
            'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
                [
                    '百度统计' => ['bdtj/home/index', 'fa fa-paw'],
                    'API设置' => ['bdtj/home/edit', 'fa fa-edit'],
                    'help' => ['674'],
                ]
            ),
        ]);
    }

    // 百度统计
    public function index() {

        $data = \Phpcmf\Service::M('bdtj', 'bdtj')->getConfig();
        if (!$data['bdtj']['user']) {
            $this->_admin_msg(0, '没有配置百度统计账号，请在插件API配置-百度统计填写', dr_url('bdtj/home/edit'));
        }

        \Phpcmf\Service::V()->assign([
            'api' => $data['bdtj'],
        ]);
        \Phpcmf\Service::V()->display('bdtj_list.html');
    }

    public function edit() {

        $post = \Phpcmf\Service::L('Input')->post('data', true);


        if (IS_AJAX_POST) {
            \Phpcmf\Service::M('bdtj', 'bdtj')->setConfig($post);
            \Phpcmf\Service::L('Input')->system_log('设置百度统计工具');
            exit($this->_json(1, dr_lang('操作成功')));
        }

        $page = intval(\Phpcmf\Service::L('Input')->get('page'));

        \Phpcmf\Service::V()->assign([
            'page' => $page,
            'data' => \Phpcmf\Service::M('bdtj', 'bdtj')->getConfig(),
            'form' => dr_form_hidden(['page' => $page]),
        ]);
        \Phpcmf\Service::V()->display('config.html');
    }

    public function site_index() {

        $rt = \Phpcmf\Service::M('bdtj', 'bdtj')->get_site_list();
        if (!$rt['code']) {
            $this->_json(0, $rt['msg']);
        }

        $html = '';
        foreach ($rt['data'] as $r) {
            foreach ($r['list'] as $t) {
                $info = '';
                $c = \Phpcmf\Service::M('bdtj', 'bdtj')->get_overview($t['site_id']);
                if (!$c['code']) {
                    $info = $rt['msg'];
                } else {
                    foreach ($c['data'] as $tt) {
                        $info.= '<p style="margin: 10px 0">'.$tt['time'].'&nbsp;&nbsp;&nbsp;浏览量(PV)：'.$tt['pv'].'，访客数(UV)：'.$tt['uv'].'，IP数：'.$tt['ip'].'</p>';
                    }
                }
                $html.= '<tr>';
                $html.= '<td>'.$t['site_id'].'</td>';
                //$html.= '<td><a href="'.dr_url('zhanzhang/bdtj/show', ['id' => $t['site_id']]).'">'.$t['domain'].'</a></td>';
                $html.= '<td>'.$t['domain'].'</td>';
                $html.= '<td>'.(!$t['status'] ? '正常' : '暂停').'</td>';
                $html.= '<td>'.$info.'</td>';
                $html.= '</tr>';
            }
        }
        $this->_json(1, $html);
    }


}
